#!/bin/bash

DEST=$1
ENV_FILE=$1/shavenmaven-jarjar-prototype.env

if [ ! -f $ENV_FILE ];
then
    echo "Could not find $ENV_FILE"
    exit 1
fi


source $ENV_FILE
if [ -f $DEST/build.xml ];
then
    mv $DEST/build.xml $DEST/build.xml.was
fi
MAIN_PACKAGE=`echo $PACKAGE_PREFIX | sed -e s#\\\.#/#g`
FULLY_QUALIFIED_MAIN_CLASS="$PACKAGE_PREFIX.$MAIN_CLASS"

echo $MAIN_PACKAGE


cat build.xml | sed -e s/__FULLY_QUALIFIED_MAIN_CLASS__/$FULLY_QUALIFIED_MAIN_CLASS/g -e s/__PROJECT_NAME__/$PROJECT_NAME/g -e s/__MAIN_CLASS__/$MAIN_CLASS/g -e s/__PACKAGE_PREFIX__/$PACKAGE_PREFIX/g -e s#__MAIN_PACKAGE__#$MAIN_PACKAGE#g > $DEST/build.xml
cp -r lib $DEST
if [ ! -f $DEST/build ];
then
    cp -r build $DEST
fi
cp build/*.xml $DEST/build
cat build/macros.xml | sed -e s/__FULLY_QUALIFIED_MAIN_CLASS__/$FULLY_QUALIFIED_MAIN_CLASS/g -e s/__PROJECT_NAME__/$PROJECT_NAME/g -e s/__MAIN_CLASS__/$MAIN_CLASS/g -e s/__PACKAGE_PREFIX__/$PACKAGE_PREFIX/g -e s#__MAIN_PACKAGE__#$MAIN_PACKAGE#g > $DEST/build/macros.xml

for d in $RUNTIME_DEPENDENCIES
do
    cat build/$d.dependencies >> $DEST/build/runtime.dependencies
done

mkdir -p $DEST/src/$MAIN_PACKAGE $DEST/test/$MAIN_PACKAGE

echo "package $PACKAGE_PREFIX;" > $DEST/src/$MAIN_PACKAGE/$MAIN_CLASS.java
cat src/Main.java >> $DEST/src/$MAIN_PACKAGE/$MAIN_CLASS.java
echo "package $PACKAGE_PREFIX;" > $DEST/test/$MAIN_PACKAGE/$MAIN_CLASS"Test.java"
cat test/MainTest.java >> $DEST/test/$MAIN_PACKAGE/$MAIN_CLASS"Test.java"

